package com.huicoo.forestmanager.widget.update;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.huicoo.forestmanager.R;

public class DownDialog {
    private Dialog dialog;
    private DownDialogListener dialogClickListener;
    private ProgressBar view_progress;
    private TextView tv_title, tv_progress;

    /***
     * 刷新dialog
     *
     * @param progress
     *            进度
     * @param desc
     *            下载秒速
     * @param speed
     *            下载速度
     */
    public void updateView(int progress, String desc, long speed) {
        view_progress.setProgress(progress);
        tv_progress.setText(progress + "%");
        if (!TextUtils.isEmpty(desc) && desc.contains("异常")) {
            tv_title.setText("下载异常，请重试");
        }
    }

    public DownDialog(final Context context) {
        if (dialog == null) {
            dialog = new Dialog(context, R.style.transDialog);
        }
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_app_update_downloading);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (dialogClickListener != null) {
                    dialogClickListener.noSure();
                    dismiss();
                }
            }
        });

        tv_title = dialog.findViewById(R.id.tv_download_title);
        view_progress = dialog.findViewById(R.id.pb_download);
        tv_progress = dialog.findViewById(R.id.tv_progress);
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        if (dialog == null) {
            return;
        }
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void setOnDialogClickListener(DownDialogListener dc) {
        dialogClickListener = dc;
    }

    public interface DownDialogListener {
        void noSure();
    }

}
